<?php
class Admin_Form_Account extends Zend_Form{

	protected $_title;
	protected $_type; // Create or Update
	protected $_submit;
	protected $_fullname;
	protected $_username;
	protected $_password;
	protected $_address;
	protected $_phone;
	protected $_email;
	protected $_role;

	public function __construct($type) {
		parent::__construct();
		$this->_type=$type;
		$this->create($this->_type);
	}
	public function create($type)
	{
		$titleStr  = '';
		$submitStr = '';
		$action = ' ';
		$url = new Zend_View_Helper_Url();
		if ($type == 'add') {
			$action = $url->url(array('module'=>'admin', 'controller'=> 'account','action'=>'addaccount'),'default');
			$submitStr = 'Add account';
			$titleStr = 'Add Account';
		} else if ($type == 'update'){
			$action = $url->url(array('module'=>'admin', 'controller'=> 'account','action'=>'updateaccount'),'default');
			$submitStr = 'Update account';
			$titleStr = 'Update Account';
		}
		$this->setName($submitStr);
		$this->setAttrib('enctype', 'multipart/form-data');
		$this->setAction($action);

		//title
		$this->_title = new Zend_Form_Decorator_Captcha();
		$this->_title->render($titleStr);

		//user's info
		$this->_fullname = new Zend_Form_Element_Text('fullname');
		$this->_fullname->setLabel('Full name ')
		->setRequired(true)
		->addValidator('NotEmpty');

		$this->_username = new Zend_Form_Element_Text('username');
		$this->_username->setLabel('User Name ')
		->setRequired(false);
		if ($type == 'add') {
			$this->_username->addValidator(new UserNameValidate());
			$this->_username->setRequired(true);
		} else {
			$this->_username->setAttrib('READONLY', true);
		}
		
		

		$this->_password = new Zend_Form_Element_Text('password');
		$this->_password->setLabel('Password ')
		->setRequired(true)
		->addValidator('NotEmpty');

		$this->_address = new Zend_Form_Element_Text('address');
		$this->_address->setLabel('Address ')
		->setRequired(true)
		->addValidator('NotEmpty');

		$this->_phone = new Zend_Form_Element_Text('phone');
		$this->_phone->setLabel('Phone ')
		->setRequired(true)
		->addValidator('NotEmpty');

		$this->_email = new Zend_Form_Element_Text('email');
		$this->_email->setLabel('Email ')
		->setRequired(true)
		->addValidator('NotEmpty');

		$this->_role = new Zend_Form_Element_Select('role');
		$this->_role->setLabel('Role ')
		->setMultiOptions(array('2' => 'User',
						   '1' => 'Admin'));

		$this->_submit = new Zend_Form_Element_Submit('submit');
		$this->_submit->setLabel($submitStr);

		$this->addElements(array($this->_title, $this->_fullname, $this->_username, $this->_password, $this->_address, $this->_phone, $this->_email, $this->_role, $this->_submit));

	}

	public function populateData(Front_Model_User $userinfo) {
		if (isset($userinfo->_fullname)) {
			$this->_fullname->setValue($userinfo->_fullname);
		}
		if (isset($userinfo->_username)) {
			$this->_username->setValue($userinfo->_username);
		}
		if (isset($userinfo->_password)) {
			$this->_password->setValue($userinfo->_password);
		}
		if (isset($userinfo->_address)) {
			$this->_address->setValue($userinfo->_address);
		}
		if (isset($userinfo->_phone)) {
			$this->_phone->setValue($userinfo->_phone);
		}
		if (isset($userinfo->_email)) {
			$this->_email->setValue($userinfo->_email);
		}
		if (isset($userinfo->_role)) {
			$this->_role->setValue($userinfo->_role);
		}
		//		if (isset($userinfo->_remain)) {
		//			$this->_remain->setValue($userinfo->_remain);
		//		}
	}
}

class UserNameValidate extends Zend_Validate_Abstract {
	
	const EXIST = 'exist';
	
	protected $_messageTemplates = array(
		self::EXIST => "'%value%' has exist"
	);
	public function isValid($value)
	{
		$userMapper = new Front_Model_UserMapper();
		if (!$userMapper->isValidUsername($value)) {
			$this->_error(self::EXIST, $value);
			return false;
		}
		return true;
	}
}